package com.htgd.gardenexpo.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：hejing
 * @date ：Created in 2021/8/13 16:04
 * @description：天气工具类
 * @modified By：`
 * @version: 1.0
 */
@Component
public class WeatherUtils {

    @Value("${local.url}")
    public String local_url;
    /**
     *
     * 获取实时天气2<br>
     * 方 法 名： getTodayWeather <br>
     */
    public static JSONObject getTodayWeather(String city)
            throws IOException, NullPointerException, MalformedURLException {
        // 连接中央气象台的API
        URL url = new URL(city);
        URLConnection connectionData = url.openConnection();
        connectionData.setConnectTimeout(1000);
        JSONObject info = new JSONObject();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    connectionData.getInputStream(), "UTF-8"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = br.readLine()) != null)
                sb.append(line);
            String datas = sb.toString();
            System.out.println(datas);
            JSONObject jsonData = JSON.parseObject(datas);
            info = jsonData.getJSONObject("weatherinfo");


        } catch (SocketTimeoutException e) {
            System.out.println("连接超时");
        } catch (FileNotFoundException e) {
            System.out.println("加载文件出错");
        }

        return info;

    }

    public JSONObject addLogo (JSONObject weatherData){
        //判断当前天气情况
        String weather=weatherData.getString("weather");

        switch (weather){
            case "多云":
                weatherData.put("url",local_url+"weather/阴.png");
                weatherData.put("logo",local_url+"weather/多云-2x.png.png");
                break;
            case "阴":
                weatherData.put("url",local_url+"weather/阴.png");
                weatherData.put("logo",local_url+"weather/阴-2x.png.png");
                break;
            case "雨":
                weatherData.put("url",local_url+"weather/雨.png");
                weatherData.put("logo",local_url+"weather/小雨-2x.png.png");
            case "小雨":
                weatherData.put("url",local_url+"weather/雨.png");
                weatherData.put("logo",local_url+"weather/小雨-2x.png.png");
            case "中雨":
                weatherData.put("url",local_url+"weather/雨.png");
                weatherData.put("logo",local_url+"weather/中雨-2x.png.png");
            case "大雨":
                weatherData.put("url",local_url+"weather/雨.png");
                weatherData.put("logo",local_url+"weather/大雨-2x.png.png");
            case "雷阵雨":
                weatherData.put("url",local_url+"weather/雨.png");
                weatherData.put("logo",local_url+"weather/雷阵雨-2x.png.png");
            case "暴雨":
                weatherData.put("url",local_url+"weather/雨.png");
                weatherData.put("logo",local_url+"weather/暴雨-2x.png.png");
            case "雨夹雪":
                weatherData.put("url",local_url+"weather/雨.png");
                weatherData.put("logo",local_url+"weather/雨夹雪-2x.png.png");
                break;
            case "雪":
                weatherData.put("url",local_url+"weather/雪.png");
                weatherData.put("logo",local_url+"weather/小雪-2x.png.png");
                break;
            case "小雪":
                weatherData.put("url",local_url+"weather/雪.png");
                weatherData.put("logo",local_url+"weather/小雪-2x.png.png");
                break;
            case "中雪":
                weatherData.put("url",local_url+"weather/雪.png");
                weatherData.put("logo",local_url+"weather/中雪-2x.png.png");
                break;
            case "大雪":
                weatherData.put("url",local_url+"weather/雪.png");
                weatherData.put("logo",local_url+"weather/大雪-2x.png.png");
                break;
            case "暴雪":
                weatherData.put("url",local_url+"weather/雪.png");
                weatherData.put("logo",local_url+"weather/暴雪-2x.png.png");
                break;
            default:
                weatherData.put("url",local_url+"weather/晴.png");
                weatherData.put("logo",local_url+"weather/晴-2x.png.png");
                break;
        }

        return weatherData;
    }
}
